package com.treasure.hunt.popup

import android.content.Context
import android.view.View
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import android.widget.ImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.module.lemlin.base.loadCropImage
import com.treasure.hunt.R
import com.treasure.hunt.app.AppConstance
import com.treasure.hunt.entity.SignList
import com.treasure.hunt.entity.TaskSign
import kotlinx.android.synthetic.main.popup_dialog_sign.view.*
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AlphaConfig
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.ScaleConfig

class DialogSignPopup(
    context: Context,
    val onConfirm: (view: View) -> Unit
) : BasePopupWindow(context) {

    init {
        setBlurBackgroundEnable(true)
    }

    private var signNum: Int = 0
    private var isSign: Int = 0

    private val goodsAdapter =
        object : BaseQuickAdapter<SignList, BaseViewHolder>(R.layout.item_goods_sign) {

            private val signDayImage: MutableList<Int> = mutableListOf(
                R.mipmap.ic_sign_day_1,
                R.mipmap.ic_sign_day_2,
                R.mipmap.ic_sign_day_3,
                R.mipmap.ic_sign_day_4,
                R.mipmap.ic_sign_day_5,
                R.mipmap.ic_sign_day_6,
                R.mipmap.ic_sign_day_7
            )

            override fun convert(holder: BaseViewHolder, item: SignList) {
                holder.setText(R.id.tvSignTitle, item.title)
                val isHttps: Boolean = item.image.substring(0, 5).contains("http") ||
                        item.image.substring(0, 5).contains("https")
                holder.getView<ImageView>(R.id.ivSignIcon)
                    .loadCropImage("${if (isHttps) "" else AppConstance.URL_IMAGE}${item.image}")
                val position: Int = holder.layoutPosition
                holder.getView<ImageView>(R.id.ivSignAngle)
                    .setImageResource(signDayImage[position])
                when {
                    signNum > position -> holder.setGone(R.id.ivSignAngle, (false))
                    signNum == position -> holder.setGone(R.id.ivSignAngle, (isSign == 0))
                    else -> holder.setGone(R.id.ivSignAngle, (true))
                }
            }
        }

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_dialog_sign)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.tvDialogSign.setOnClickListener {
            if (isSign == 1) {
                dismiss()
            } else {
                onConfirm(it)
            }
        }
        contentView.tvDialogDoubled.setOnClickListener {
            onConfirm(it)
        }
        contentView.rvDialogSignClose.setOnClickListener {
            dismiss()
        }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withScale(ScaleConfig.CENTER)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withScale(ScaleConfig.CENTER)
            .toDismiss()
    }

    fun setFillData(sign: TaskSign): DialogSignPopup {
        this.signNum = sign.sign_num ?: 0
        this.isSign = sign.is_sign
        if (sign.is_sign == 1) {
            contentView.tvDialogSign.text = "已签到"
            contentView.tvDialogSign.setBackgroundResource(R.mipmap.ic_23288)
            val list: MutableList<SignList> = sign.list
            if (list.isNullOrEmpty()) {
                contentView.tvDialogSign.visibility = View.VISIBLE
                contentView.tvDialogDoubled.visibility = View.GONE
            } else if (list[signNum].double_switch == 1) {
                contentView.tvDialogSign.visibility = View.GONE
                contentView.tvDialogDoubled.visibility = View.VISIBLE
            } else {
                contentView.tvDialogSign.visibility = View.VISIBLE
                contentView.tvDialogDoubled.visibility = View.GONE
            }
        } else {
            contentView.tvDialogSign.text = "签到"
            contentView.tvDialogSign.setBackgroundResource(R.mipmap.ic_20426)
            contentView.tvDialogDoubled.visibility = View.GONE
        }
        contentView.rvDialogSignBody.adapter =
            goodsAdapter.apply {
                setNewInstance(sign.list)
            }
        return this
    }
}